from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.EmployeeAdminController import EmployeeAdminController
from controller.PublicAdminController import PublicAdminController
from schema.QuerySchema import QueryBase, EmployeeQuery
from schema.TeachingSchema import EmployeeInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

employee_admin_router = APIRouter()


@employee_admin_router.post("/employee/add", summary='添加雇员信息')
def add_employee(employeeInfo: EmployeeInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employee/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employee/add', db):
            response = EmployeeAdminController.add_employee(employeeInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@employee_admin_router.post("/employee/mod", summary='根据雇员id修改雇员信息')
def mod_employee(employeeInfo: EmployeeInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employee/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employee/mod', db):
            response = EmployeeAdminController.mod_employee(employeeInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@employee_admin_router.post("/employee/querybypage", summary='分页查询所有雇员信息')
def query_employee_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employee/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employee/query', db):
            response = EmployeeAdminController.query_employee_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@employee_admin_router.post("/employee/query", summary='根据phone、name、port字段分页查询雇员信息')
def query_employee_by_field(employeeQuery: EmployeeQuery, db: Session = Depends(get_db_session),
                            result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employee/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employee/query', db):
            response = EmployeeAdminController.query_employee_by_field(employeeQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@employee_admin_router.get("/employee/querybyport/{port_id}", summary='根据port字段查询雇员信息')
def query_employee_by_port_id(port_id: int, db: Session = Depends(get_db_session),
                              result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employee/querybyport/port_id={port_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employee/query', db):
            response = EmployeeAdminController.query_employee_by_port_id(port_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result

@employee_admin_router.get("/employee/query/allports", summary='按照port查询所有雇员信息并进行分组')
def query_employee_all_group_by_port(db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/employee/query/allports')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/employee/query', db):
            response = EmployeeAdminController.query_employee_all_group_by_port(db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
